package com.csweimei.rentmanagementtreasure.tenant.viewModel;

import android.app.Application;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.csweimei.rentmanagementtreasure.LoginActivity;
import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.base.AppConfig;
import com.csweimei.rentmanagementtreasure.enterprise.act.HomeEnterActivity;
import com.csweimei.rentmanagementtreasure.http.Repository;
import com.csweimei.rentmanagementtreasure.http.entity.AuthBean;
import com.csweimei.rentmanagementtreasure.http.entity.BaseResult;
import com.csweimei.rentmanagementtreasure.http.entity.BindEntEntity;
import com.csweimei.rentmanagementtreasure.http.entity.UserInfoEntity;
import com.csweimei.rentmanagementtreasure.http.entity.VipBean;
import com.csweimei.rentmanagementtreasure.pop.PopUtils;
import com.csweimei.rentmanagementtreasure.pop.WarnPop;
import com.csweimei.rentmanagementtreasure.tenant.ui.act.VipPersonActivity;
import com.csweimei.rentmanagementtreasure.tenant.ui.act.dayPaper.DayPaperActivity;
import com.csweimei.rentmanagementtreasure.tenant.ui.act.dayPaper.SearchTeamActivity;
import com.csweimei.rentmanagementtreasure.tenant.ui.act.mine.PersonCheckActivity;
import com.csweimei.rentmanagementtreasure.tenant.ui.act.mine.PersonInfoActivity;
import com.csweimei.rentmanagementtreasure.tenant.ui.act.mine.SetActivity;
import com.csweimei.rentmanagementtreasure.utils.UserUtils;
import com.csweimei.rentmanagementtreasure.utils.ValidateUtils;
import com.example.feedback.activity.SuggestListActivity;
import com.example.feedback.service.FeedBackInit;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.internal.operators.observable.ObservableFilter;
import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.SPUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class FFourVM extends BaseViewModel<Repository> {

    public FFourVM(@NonNull Application application, Repository model) {
        super(application, model);
    }

    public ObservableField<String> vip1 = new ObservableField<>("开通产品服务享受更多功能");
    public ObservableField<String> vip2 = new ObservableField<>("去开通");



    @Override
    public void onResume() {
        super.onResume();
        if (model.isLogin()){
            getUserData();
            getEntData();
            getVipInfo();
        }else {
            name.set("请先登录");
            imgUrl.set("");
        }
    }

    private void getVipInfo() {
        Map<String,String> map = new HashMap<>();
        map.put("userid",model.getLoginInfo().getData().getUser_id()+"");
        model.seach_vip(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<VipBean>() {
                    @Override
                    public void onNext(VipBean o) {
                        if (o.issucc){
                            if (o.getData().getDay() > 0){
                                AppConfig.setVipPerson(o.getData().getDay());
                                vip1.set(ValidateUtils.getTimeByType(o.getData().getEtime(),ValidateUtils.TIME_YMD)+"到期");
                                vip2.set("去续费");
                            }else {
                                vip1.set("开通产品服务享受更多功能");
                                vip2.set("去开通");
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public ObservableField<String> bindEntName = new ObservableField<>("未绑定公司");
    public ObservableField<String> addText = new ObservableField<>("去添加");

    public ObservableField<String> entLogo = new ObservableField<>("");

    public boolean isEnt = false;

    public ObservableField<BindEntEntity.DataDTO> entData = new ObservableField<>();
    private void getEntData() {
        Map<String,String> map = new HashMap<>();
        map.put("userid",model.getLoginInfo().getData().getUser_id());
        model.isbindEnt(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<BindEntEntity>() {
                    @Override
                    public void onNext(BindEntEntity bean) {
                        isEnt = bean.issucc;
                        if (bean.issucc){
                            entData.set(bean.getData());
                            bindEntName.set(bean.getData().getCompanyName());
                            addText.set("");
                            entLogo.set(bean.getData().getCompanylogo());
                        }else {
                            bindEntName.set("未绑定公司");
                            addText.set("去添加");
                            entLogo.set("");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private Boolean authState =false;
    public ObservableField<Integer> imgUrlRz = new ObservableField<>(R.mipmap.rz_2);

    public ObservableField<String> rzStata  = new ObservableField<>("未认证");

    private void getCheckData() {
        Map<String,String> map = new HashMap<>();
        map.put("userid",model.getLoginInfo().getData().getUser_id());
        model.seachauth(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<AuthBean>() {
                    @Override
                    public void onNext(AuthBean bean) {
                        if (bean.issucc){
                            authState = bean.getData().getStatus() == 0;
                            imgUrlRz.set(authState?R.mipmap.rz_2:R.mipmap.rz_1);
                            rzStata.set(authState?"未认证":"已认证");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void getUserData() {
        Map<String,String> map = new HashMap<>();
        map.put("userid",model.getLoginInfo().getData().getUser_id());
        model.userinfo(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<UserInfoEntity>() {
                    @Override
                    public void onNext(UserInfoEntity userInfoEntity) {
                        model.setUserInfo(userInfoEntity);
                        setViewValue(userInfoEntity);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public ObservableField<String> name = new ObservableField<>("您还未登录");
    public ObservableField<String> imgUrl = new ObservableField<>();

    private void setViewValue(UserInfoEntity userInfoEntity) {
        if (ValidateUtils.isNotEmptyObjectOrString(userInfoEntity)){
            if (ValidateUtils.isNotEmptyObjectOrString(userInfoEntity.getData())){
                name.set(userInfoEntity.getData().getName());
                imgUrl.set(userInfoEntity.getData().getHeadimg());
            }


        }


    }


    public BindingCommand loginClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (!model.isLogin()){
                startActivity(LoginActivity.class);
            }

        }
    });

    public BindingCommand infoClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (model.isLogin()){
                startActivity(PersonInfoActivity.class);
            }else {
                startActivity(LoginActivity.class);
            }

        }
    });

    public BindingCommand lxkf = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            WarnPop warnPop = new WarnPop(mActivity, "客服QQ群", "965723047","复制", new WarnPop.OnSelCallBack() {
                @Override
                public void onCallBack(String s) {
                    //获取剪贴板管理器：
                    ClipboardManager cm = (ClipboardManager) mActivity.getSystemService(Context.CLIPBOARD_SERVICE);
                    // 创建普通字符型ClipData
                    ClipData mClipData = ClipData.newPlainText("Label","965723047");
                    // 将ClipData内容放到系统剪贴板里。
                    cm.setPrimaryClip(mClipData);
                    ToastUtils.showShort("复制成功");
                }
            });
            PopUtils.showPop(mActivity,warnPop,false);

        }
    });

    public BindingCommand helpClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
        }
    });

    public BindingCommand feedClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (model.isLogin()){
                FeedBackInit.getInstance().init(new FeedBackInit.FeedBack() {
                    @Override
                    public String getName() {
                        return model.getLoginInfo().getData().getNickname();
                    }

                    @Override
                    public String getAccount() {
                        return model.getLoginInfo().getData().getUser_id();
                    }

                    @Override
                    public String getAppName() {
                        return AppConfig.APP_NAME;
                    }
                });
                startActivity(SuggestListActivity.class);
            }else {
                startActivity(LoginActivity.class);
            }

        }
    });

    public BindingCommand setClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            startActivity(SetActivity.class);
        }
    });

    public BindingCommand vipClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (model.isLogin()){
                startActivity(VipPersonActivity.class);
            }else {
                startActivity(LoginActivity.class);
            }
        }
    });

    public BindingCommand authClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            startActivity(PersonCheckActivity.class);
        }
    });

    public BindingCommand searchEnt = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (model.isLogin()){
                if (!isEnt){
                    startActivity(SearchTeamActivity.class);
                }else {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(AppConfig.BUNDLE_KEY,entData.get());
                    startActivity(DayPaperActivity.class,bundle);
                }
            }else {
                startActivity(LoginActivity.class);
            }

        }
    });


}
